package com.ays.service;

import com.ays.bean.AtmOperationMaster;
import com.ays.mapper.AtmOperationMasterMapper;
import com.ays.ws.OPERATIONSystemService;
import com.ays.ws.OPERATIONSystemServiceSoap;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2024/6/19
 * @desc 手术回写
 */
@Service
@Slf4j
public class HisBackService {

    @Autowired
    private HisRequestHandleService hisRequestHandleService;

    @Autowired
    private AtmOperationMasterMapper atmOperationMasterMapper;

    public Map<String,Object> addOperEcho(@RequestBody List<Map<String,String>> list){
        for(Map<String,String> item : list){
            AtmOperationMaster param = new AtmOperationMaster();
            param.setOperId(item.get("scheduleId"));
            param.setPatientId(item.get("patientId"));
            param.setVisitId(item.get("visitId"));
            List<AtmOperationMaster> atmOperationMasters = atmOperationMasterMapper.selectList(param);
            AtmOperationMaster master = atmOperationMasters.get(0);

            if("0".equals(String.valueOf(master.getOperStatus()))){
                // 提交排班
                // 排班回写
                String entity = hisRequestHandleService.arrangeBack(item);
                String request = "<Request><Header><SourceSystem>AN</SourceSystem><MessageID/></Header><Body><Arguments/><Struct><Models><Model d5p1:type=\"Model\" xmlns:d5p1=\"http://www.w3.org/2001/XMLSchema-instance\">"+entity+"</Model></Models></Struct></Body></Request>";
                webserviceClient("MES0029",request);
            }else if("5".equals(String.valueOf(master.getOperStatus()))){
                // 入手术室
                //手术状态回写
                item.put("status","CISANAI");
                String entity = hisRequestHandleService.writeBack(item);
                String request = "<Request><Header><SourceSystem>AN</SourceSystem><MessageID/></Header><Body><StatusParameter>"+entity+"</StatusParameter></Body></Request>";
                webserviceClient("MES0034",request);
            }else if("35".equals(String.valueOf(master.getOperStatus()))){
                // 出手术室
                // 手术状态回写
                item.put("status","CISANAO");
                String entity = hisRequestHandleService.writeBack(item);
                String request = "<Request><Header><SourceSystem>AN</SourceSystem><MessageID/></Header><Body><StatusParameter>"+entity+"</StatusParameter></Body></Request>";
                webserviceClient("MES0034",request);

                // 手术登记回写
                String registrationBack = hisRequestHandleService.registrationBack(item);
                request = "<Request><Header><SourceSystem>AN</SourceSystem><MessageID/></Header><Body><StatusParameter>"+registrationBack+"</StatusParameter></Body></Request>";
                webserviceClient("MES0031",request);
            }
        }
        return null;
    }

    public String webserviceClient(String code,String param){
        log.info("请求:{}-{}",code,param);
        String s = null;
//        OPERATIONSystemService service= new OPERATIONSystemService();
//        OPERATIONSystemServiceSoap soap = service.getOPERATIONSystemServiceSoap();
//        s = soap.hipMessageServer(code, param);
//        log.info("返回:{}",s);
        return s;
    }
}
